﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Delay;
using System.ComponentModel;

namespace PhoneInfo
{
    public partial class ProfectSearch : PhoneApplicationPage
    {
        private SearchResultView profectSearch = null;
        private string key = null;
        bool ispk = false;
        bool isnull = false;
        string pid_1;
        string ppid_1;
        string price_1;
        string picurl_1;
        string phonename_1;
        string ppname_1;

        public ProfectSearch()
        {
            InitializeComponent();
        }
        void searchResultNewView_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (sender == null) return;
            SearchResultView se = sender as SearchResultView;
            if (se != null) return;
        }
        private void ListBox_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            if (sender == null) return;
            ListBox lb = sender as ListBox;
            if (lb == null) return;
            phonePkItem pkItem = lb.SelectedItem as phonePkItem;
            if (pkItem == null) return;
            string objective = "";
            if (ispk && !isnull)
            {
                objective = "/Page/PhonePk.xaml?type=1";
                objective += "&pid1=" + pid_1;
                objective += "&ppid1=" + ppid_1;
                objective += "&price1=" + price_1;
                objective += "&picurl1=" + picurl_1;
                objective += "&phonename1=" + phonename_1;
                objective += "&ppname1=" + ppname_1;
                objective += "&name1=" + ppname_1 + "\r\n" + phonename_1;
                objective += "&pid2=" + pkItem.Ponhe1_pid;
                objective += "&ppid2=" + pkItem.Ponhe1_ppid;
                objective += "&picurl2=" + pkItem.Ponhe1_picurl;
                objective += "&name2=" + pkItem.Ponhe1_ppname + "\r\n" + pkItem.Ponhe1_phonename;
                objective += "&phonename2=" + pkItem.Ponhe1_phonename;
                objective += "&ppname2=" + pkItem.Ponhe1_ppname;
            }
            else if (ispk && isnull)
            {
                objective = "/Page/PhonePk.xaml?type=1";
                objective += "&isnull=1";
                objective += "&pid1=" + pkItem.Ponhe1_pid;
                objective += "&name1=" + pkItem.Ponhe1_ppname + "\r\n" + pkItem.Ponhe1_phonename;
                objective += "&phonename1=" + pkItem.Ponhe1_phonename;
                objective += "&ppname1=" + pkItem.Ponhe1_ppname;
                objective += "&ppid=" + pkItem.Ponhe1_ppid;
                objective += "&picurl1=" + pkItem.Ponhe1_picurl;
            }
            else if (!ispk && !isnull)
            {
                objective = "/Page/TypeData.xaml?type=1";
                objective += "&pid=" + pkItem.Ponhe1_pid;
                objective += "&phonename=" + pkItem.Ponhe1_phonename;
                objective += "&price=" + pkItem.Ponhe1_price;
                objective += "&maxprice=" + pkItem.Ponhe1_maxprice;
                objective += "&minprice=" + pkItem.Ponhe1_minprice;
                objective += "&picurl=" + pkItem.Ponhe1_picurl;
                objective += "&ppname=" + pkItem.Ponhe1_ppname;
            }
            NavigationService.Navigate(new Uri(objective, UriKind.Relative));
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            IDictionary<string, string> parameters = this.NavigationContext.QueryString;
            ispk = parameters.ContainsKey("ispk");
            isnull = parameters.ContainsKey("isnull");
            if (ispk && !isnull)
            {
                pid_1 = NavigationContext.QueryString["pid_1"];
                ppid_1 = NavigationContext.QueryString["ppid_1"];
                price_1 = NavigationContext.QueryString["price_1"];
                picurl_1 = NavigationContext.QueryString["picurl_1"];
                phonename_1 = NavigationContext.QueryString["phonename_1"];
                ppname_1 = NavigationContext.QueryString["ppname_1"];
            }
            if (parameters.ContainsKey("valuekey")) key = parameters["valuekey"];
            profectSearch = new SearchResultView();
            profectSearch.ClassName = "profectSearch";
            profectSearch.PropertyChanged += new PropertyChangedEventHandler(searchResultNewView_PropertyChanged);
            profectSearch.Result = 0;
            profectSearch.CurrentPage = 1;
            profectSearch.TotalPage = 0;
            profectSearch.PageSize = 20;
            profectSearch.AllowAdd = false;
            profectSearch.SetValue(LowProfileXmlLoader.UriSourceProperty, new Uri(profectSearch.GetInitUrl(key)));
            Hot.DataContext = profectSearch;
        }
    }
}